package cc.lj.internal.coupons.domain.ticket.service.impl.useTicketRule;

import cc.lj.internal.api.ordershare.dto.OrderOnlineAPIDTO;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchMerchantListDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketMainDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketBatchMerchantListRepository;
import cc.lj.internal.coupons.domain.ticket.service.AbstractUseTicketRuleDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchMerchantListPO;
import cc.lj.internal.coupons.interfaces.dto.command.UseTicketCheckCommand;
import lj.internal.dict.enums.ticketBatch.EnumTicketMerchantRangeStatus;
import lj.internal.dict.enums.ticketBatch.EnumTicketStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用券验证规则 - 黑白名单商户
 * @author liangxifeng
 * @date 2023/6/26 15:12
 */
@Service("useTicketBlackWhiteMerchantRuleDomainServiceImpl")
@Slf4j
public class UseTicketBlackWhiteMerchantRuleDomainServiceImpl extends AbstractUseTicketRuleDomainService {
    //黑白名单商户 仓储接口
    @Resource(name = "ticketBatchMerchantListRepository")
    private ITicketBatchMerchantListRepository batchMerchantListRepository;

    @Override
    protected void rule(TicketBatchDO batchDO, TicketMainDO ticketMainDO, UseTicketCheckCommand ticketCheckCommand, OrderOnlineAPIDTO orderOnlineAPIDTO) {
        log.info("【电子券用券验证】：黑白名单商户规则验证开始==");
        TicketBatchMerchantListPO merchantListPOParam = new TicketBatchMerchantListPO();
        //通过商户K代码和电子券批次主键查询黑白名单表
        merchantListPOParam.setMerchantKid(ticketCheckCommand.getMerchantKcode()); //k代码
        merchantListPOParam.setMerchantBatchId(batchDO.getBatchId()); //批次主键
        List<TicketBatchMerchantListPO> merchantList = batchMerchantListRepository.getList(merchantListPOParam);

        //如果券批次设置的是黑名单商户 且 该商户在黑白名单商户表中存在数据, （提示：该商户是黑名单商户）
        if(batchDO.getBatchMerchantRange() == EnumTicketMerchantRangeStatus.BLACK.getCode() && merchantList.size() > 0) {
            throw new ServiceException(CoupousExceptionEnum.USE_TICKET_CHECK_IS_BLACK);
        }

        //如果券批次设置的是白名单 且 该商户不在黑白名单表中（提示：该商户不在白名单商户中）
        if(batchDO.getBatchMerchantRange() == EnumTicketMerchantRangeStatus.WHITE.getCode() && merchantList.size() == 0) {
            throw new ServiceException(CoupousExceptionEnum.USE_TICKET_MERCHANT_NOT_IN_WHITE);
        }

        log.info("【电子券用券验证】：黑白名单商户规则验证通过==");
    }
}
